package com.zscat.mallplus.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysAdminOperLog;
import com.zscat.mallplus.build.entity.BuildingCommunity;
import com.zscat.mallplus.build.entity.UserCommunityRelate;
import com.zscat.mallplus.build.mapper.BuildingCommunityMapper;
import com.zscat.mallplus.build.mapper.UserCommunityRelateMapper;
import com.zscat.mallplus.sys.entity.SysPermission;
import com.zscat.mallplus.sys.entity.SysPost;
import com.zscat.mallplus.sys.entity.SysRole;
import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.sys.mapper.SysPermissionMapper;
import com.zscat.mallplus.sys.service.*;
import com.zscat.mallplus.ums.service.RedisService;
import com.zscat.mallplus.util.JsonUtil;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import com.zscat.mallplus.vo.Rediskey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.*;

@Slf4j
@Api(value = "用户管理", description = "", tags = {"用户管理"})
@RestController
@RequestMapping("/sys/sysPost")
public class SysPostController extends ApiController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Resource
    private ISysPostService sysPostService;

    /**
     * 岗位列表
     *
     * @param entity
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('sys:SysPost:read')")
    public Object getUserByPage(SysPost entity,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            return new CommonResult().success(sysPostService.page(new Page<SysPost>(pageNum, pageSize), new QueryWrapper<>(entity)));
        } catch (Exception e) {
            return new CommonResult().failed();
        }
    }

    /**
     * 保存岗位
     *
     * @param entity
     * @return
     */
    @PostMapping(value = "/save")
    public Object saveUser(@RequestBody SysPost entity) {
        try {
            SysPost info = sysPostService.getOne(new QueryWrapper<SysPost>().eq("post_name", entity.getPostName()));
            if (info != null) {
                return new CommonResult().failed("新增岗位'" + entity.getPostName() + "'失败，岗位名称已存在");
            }
            SysPost info1 = sysPostService.getOne(new QueryWrapper<SysPost>().eq("post_code", entity.getPostCode()));
            if (info1 != null) {
                return new CommonResult().failed("新增岗位'" + entity.getPostName() + "'失败，岗位编码已存在");
            }
            if (sysPostService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    /**
     * 修改岗位
     *
     * @param entity
     * @return
     */
    @PostMapping(value = "/update")
    public Object updateUser(@RequestBody SysPost entity) {
        try {
//            SysPost info = sysPostService.getOne(new QueryWrapper<SysPost>().eq("post_name", entity.getPostName()));
//            if (info != null) {
//                return new CommonResult().failed("修改岗位'" + entity.getPostName() + "'失败，岗位名称已存在");
//            }
//            SysPost info1 = sysPostService.getOne(new QueryWrapper<SysPost>().eq("post_code", entity.getPostCode()));
//            if (info1 != null) {
//                return new CommonResult().failed("修改岗位'" + entity.getPostName() + "'失败，岗位编码已存在");
//            }
            if (sysPostService.updateById(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    /**
     * 删除岗位
     */
    @GetMapping(value = "/delete/{postId}")
    public Object deleteUser(@PathVariable Long postId) {
        try {
            if (ValidatorUtils.empty(postId)) {
                return new CommonResult().paramFailed("id不能为空！");
            }
            SysPost user = sysPostService.getById(postId);
            sysPostService.removeById(user);
            return new CommonResult().success();
        } catch (Exception e) {
            return new CommonResult().failed();
        }
    }

    /**
     * 岗位信息
     */
    @GetMapping(value = "/{postId}")
    public Object getUserById(@PathVariable Long postId) {
        try {
            if (ValidatorUtils.empty(postId)) {
                return new CommonResult().paramFailed("id不能为空！");
            }
            SysPost sysPost = sysPostService.getById(postId);
            return new CommonResult().success(sysPost);
        } catch (Exception e) {
            return new CommonResult().failed();
        }
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public Object optionselect() {
        List<SysPost> posts = sysPostService.list(null);
        return new CommonResult().success(posts);
    }

}

